<template>
  <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工工号" prop="id">
        <el-input v-model="searchForm.empId" placeholder="员工工号" :min="0" />
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button color="#ff983f" :dark="isDark" plain icon="Download" @click="handleOut"
    >导出</el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-table
    :data="ScoreRuleList"
    border
    style="
      width: 100%;
      margin: 20px 0px;
      border-radius: 8px;
      box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
    "
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
  >
    <el-table-column prop="name" label="员工" width="100"></el-table-column>
    <el-table-column prop="position" label="岗位" width="100"></el-table-column>
	<el-table-column
	  prop="basicWage"
	  label="基础工资/￥"
	  width="130"
	></el-table-column>
    <el-table-column
      prop="scoreWage"
      label="评分结算/￥"
      width="130"
    ></el-table-column>
    <el-table-column
      prop="pieceWage"
      label="计件结算/￥"
      width="130"
    ></el-table-column>
    <el-table-column
      prop="kpiWage"
      label="提成结算/￥"
      width="130"
    ></el-table-column>
    <el-table-column
      prop="okrWage"
      label="okr结算/￥"
      width="130"
    ></el-table-column>
    <el-table-column
      prop="rewardWage"
      label="特殊绩效/￥"
      width="130"
    ></el-table-column>
	<el-table-column
	  prop="partTimeRewardWage"
	  label="兼职绩效/￥"
	  width="130"
	></el-table-column>
	<el-table-column
	  prop="correctedValue"
	  label="修正值/￥"
	  width="130"
	  v-auth="'编辑工资'"
	></el-table-column>
    <el-table-column prop="total" label="总计/￥" width="130"></el-table-column>
    <el-table-column prop="updateTime" label="最后更新时间">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>
	
	<el-table-column label="操作" width="150" v-auth="'编辑工资'">
	  <template #default="scope">
		  <!-- 未完成 先隐藏 -->
	    <el-button size="small" @click="handleEdit1(scope.$index, scope.row)" 
	      >编辑</el-button>
	  </template>
	</el-table-column>
	
	
	
	
  </el-table>
  <pagination
    class="pagination"
    v-show="ScoreListPage.total > 0"
    :total="ScoreListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="searchForm.page_size"
    @pagination="handlePagination"
  />
  
  <Dialog
    v-model="DialogVisible"
    :close="close"
    :update="update"
    :select="select1"
  ></Dialog>
  
  
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import {getEmpWageNowPage,getEmpWageNowSearch} from "@/api/empWage"
import { ElMessage } from "element-plus";
import {downLoadEmpWage} from "@/api/downExcel"
import Dialog from "./wageDialog";

const searchForm = ref({
  empId: "",
  empName: "",
  page: 1,
  page_size: 10,
});
const ScoreListPage = ref([]);
const ScoreRuleList = ref([]);
onBeforeMount(() => {
  getEmpWageNowPage(searchForm.value).then((data) => {
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
});
watch(
  ScoreRuleList,
  (newVal) => {
  },
  { deep: true }
);

const handleOut=()=>{
  downLoadEmpWage()
}

const searchEvent=()=>{
	
  getEmpWageNowSearch(searchForm.value).then((data) => {
	  
    ScoreListPage.value = data.data;
    ScoreRuleList.value = data.data.records;
  });
}

const handlePagination=(value)=>{
  if(searchForm.value.empId==""&&searchForm.value.empName==""){
	  console.log(searchForm.value)
    getEmpWageNowPage(value).then(data=>{
		console.log(data.data)
        ScoreListPage.value = data.data;
        ScoreRuleList.value = data.data.records;
    })
  }
  else{
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getEmpWageNowSearch(searchForm.value).then(data=>{
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    })
  } 
}

let select=reactive()
const DialogVisible = ref(false);
const handleEdit=(index,row)=>{
  DialogVisible.value = true;
  select=row
}
const handleDelete=(index,row)=>{
  reDelete({id:row.empScoreId}).then(data=>{
    if (data.code == 200) {
      ElMessage({
        message: "修正成功",
        type: "success",
      });
      update()
    } else {
      ElMessage.error("修正失败");
    }
  })
}

const update = () => {
  searchForm.value.empId=""
  searchForm.value.empName=""
  getEmpWageNowPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      ScoreListPage.value = data.data;
      ScoreRuleList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};

let select1=reactive()
const close=()=>{
  DialogVisible.value = false;
}

const handleEdit1=(index,row)=>{
  DialogVisible.value = true;
  select1 = row
}



</script>

<style lang="scss" scoped>
.el-card {
  border-radius: 10px;
}
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>